package com.ucode.wap.cgms.controller;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.ucode.cgms.service.CgmsAfterSaleCompanionOrderService;
import com.ucode.cgms.service.CgmsCompanionOrderService;
import com.ucode.model.cgms.CgmsAfterSaleCompanionOrder;
import com.ucode.model.cgms.CgmsCompanionOrder;
import com.ucode.model.cgms.convert.CgmsAfterSaleCompanionOrderConvert;
import com.ucode.model.cgms.enums.CgmsAfterSaleCompanionOrderStatusEnum;
import com.ucode.model.cgms.form.CgmsAfterSaleCompanionOrderApplyForm;
import com.ucode.model.cgms.vo.CgmsAfterSaleCompanionOrderVO;
import com.ucode.tool.annotation.NeedLogin;
import com.ucode.tool.base.Paginator;
import com.ucode.tool.base.ResponseResult;
import com.ucode.tool.util.ServletUtils;
import com.ucode.wap.base.core.CurrentLoginUserHandler;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@Tag(name = "移动端 - 陪玩社区系统 - 售后订单信息")
@RestController
@RequestMapping("/cgms/after-sale-order")
@Validated
public class ApiCgmsAfterSaleCompanionOrderController {

    private static Log log = LogFactory.get();
    @Resource
    private CgmsCompanionOrderService cgmsCompanionOrderService;
    @Resource
    private CgmsAfterSaleCompanionOrderService cgmsAfterSaleCompanionOrderService;
    @Resource
    private CurrentLoginUserHandler currentLoginUserHandler;

    @NeedLogin
    @Operation(summary = "分页列表")
    @GetMapping(value = "/page-list")
    public ResponseResult<Paginator<CgmsAfterSaleCompanionOrderVO>> pageList(
            @Schema(description = "页码", example = "1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @Schema(description = "每页记录数", example = "5") @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {

        Map<String,Object> condition = new HashMap<>();
        condition.put("userId",currentLoginUserHandler.getCurrentUserId());
        condition.put("deltag",false);
        Paginator<CgmsAfterSaleCompanionOrder> paginator = cgmsAfterSaleCompanionOrderService.queryPageForList(condition,pageSize,pageNum);
        Paginator<CgmsAfterSaleCompanionOrderVO> paginatorVO = CgmsAfterSaleCompanionOrderConvert.INSTANCE.convertPage(paginator);

        return ResponseResult.success(paginatorVO);
    }

    @NeedLogin
    @GetMapping("/details/{id}")
    @Parameter(description = "订单编码", name = "id", required = true, in = ParameterIn.PATH, example = "1024")
    @Operation(summary = "订单信息详情")
    public ResponseResult<CgmsAfterSaleCompanionOrderVO> details(@PathVariable(name = "id", required = true) Long id) {
        CgmsAfterSaleCompanionOrder afterSaleCompanionOrder = cgmsAfterSaleCompanionOrderService.validate(id);
        CgmsAfterSaleCompanionOrderVO vo = CgmsAfterSaleCompanionOrderConvert.INSTANCE.convert(afterSaleCompanionOrder);

        CgmsCompanionOrder cgmsCompanionOrder = cgmsCompanionOrderService.selectByPrimaryKey(afterSaleCompanionOrder.getOrderId());
        if(ObjUtil.isNotNull(cgmsCompanionOrder)){
            vo.setPayPrice(cgmsCompanionOrder.getPayPrice());
            vo.setPayTime(cgmsCompanionOrder.getPayTime());
            vo.setPayType(cgmsCompanionOrder.getPayType());
        }

        return ResponseResult.success(vo);
    }


    @NeedLogin(validate = true)
    @PostMapping("/submit")
    @Operation(summary = "提交申请")
    public ResponseResult<Long> submit(@Valid @RequestBody CgmsAfterSaleCompanionOrderApplyForm form) {
        CgmsAfterSaleCompanionOrder bean = CgmsAfterSaleCompanionOrderConvert.INSTANCE.convert(form);
        Long userId = currentLoginUserHandler.getCurrentUserId();
        CgmsCompanionOrder cgmsCompanionOrder = cgmsCompanionOrderService.validate(bean.getOrderId(), userId);
        bean.setUserId(userId);
        bean.setUserIp(ServletUtils.getClientIP());
        bean.setZoneId(cgmsCompanionOrder.getZoneId());
        bean.setCategoryId(cgmsCompanionOrder.getCategoryId());
        bean.setProductId(cgmsCompanionOrder.getProductId());
        bean.setTitle(cgmsCompanionOrder.getTitle());
        bean.setSubTitle(cgmsCompanionOrder.getSubTitle());
        bean.setPicUrl(cgmsCompanionOrder.getPicUrl());
        bean.setMarketPrice(cgmsCompanionOrder.getMarketPrice());
        bean.setPrice(cgmsCompanionOrder.getPrice());
        bean.setReturnName(cgmsCompanionOrder.getScheduledName());
        bean.setReturnPhone(cgmsCompanionOrder.getScheduledContact());
        bean.setStatuz(CgmsAfterSaleCompanionOrderStatusEnum.APPLY.getStatus());
        bean.setPayStatementId(cgmsCompanionOrder.getPayOrderId());
        cgmsAfterSaleCompanionOrderService.apply(bean);

        return ResponseResult.success();
    }

    @NeedLogin(validate = true)
    @PostMapping("/cancel/{id}")
    @Parameter(description = "订单编号", name = "id", required = true, in = ParameterIn.PATH, example = "1024")
    @Operation(summary = "取消订单")
    public ResponseResult cancel(@PathVariable(name = "id", required = true) Long id) {
        Long userId = currentLoginUserHandler.getCurrentUserId();
        cgmsAfterSaleCompanionOrderService.cancelByMember(userId,id);
        return ResponseResult.success();
    }

    @NeedLogin(validate = true)
    @PostMapping("/delete/{id}")
    @Parameter(description = "订单编号", name = "id", required = true, in = ParameterIn.PATH, example = "1024")
    @Operation(summary = "删除订单")
    public ResponseResult delete(@PathVariable(name = "id", required = true) Long id) {
        Long userId = currentLoginUserHandler.getCurrentUserId();
        cgmsAfterSaleCompanionOrderService.cancelByMember(userId,id);
        return ResponseResult.success();
    }


}
